package com.tx.review.serializable_;

import java.io.*;

/**
 *
 */
public class SerializableDemo {

    public static void main(String[] args) throws Exception {
        //序列化
        testSerialization();

        //反序列化
        Student stu = new Student();
        //序列化进去为mercury，然后再把student的name改成hhhh
        stu.setStuname("hhhh");
        testDeserialization(); //Student{stuno='10001', stuname='hhhh', stuage=0}  -->打印出来的不是序列化进去的值，说明static变量不参与反序列化

    }

    private static void testDeserialization() throws IOException, ClassNotFoundException {
        String file = "D:/test.txt";
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        Student stu = (Student)ois.readObject();
        System.out.println(stu);
        ois.close();
    }

    private static void testSerialization() throws IOException {
        String file = "D:/test.txt";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        Student stu = new Student("10001", "mercury", 20);
        oos.writeObject(stu);
        oos.flush();
        oos.close();
    }
}